package com.market.controller;


import com.market.auto.JSONAuthentication;
import com.market.entity.MaInfomation;
import com.market.entity.MaNotice;
import com.market.mapper.MaNoticeMapper;
import com.market.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author market
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/ma-notice")
public class MaNoticeController  extends JSONAuthentication {
    @Autowired
    MaNoticeMapper maNoticeMapper;
    @ApiOperation(value = "添加通知")
    @PostMapping("/add")
    public void addInfomation(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("content") String content) throws Exception{
        MaNotice maNotice=new MaNotice();
        maNotice.setUserId(id);
        maNotice.setContent(content);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        maNotice.setCreatTime(timestamp);
        if(maNoticeMapper.insert(maNotice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除通知")
    @PostMapping("/del")
    public void delInfomation(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=maNoticeMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

